// pages/cart/index.js
const {
  post,
  get
} = getApp();
Page({
  data: {
    allChecked: false,
    disabled: true,
    allPrice: 0,
    dataList: [],
    showPage: false
  },
  // 获取数据
  getList() {
    post('/cart/data').then(res => {
      if (res.result == 1) {
        this.setData({
          dataList: res.data.cartList,
          showPage: true
        })
        this.calculationPrice();
      };
    });
  },
  // 单选
  onChange(e) {
    post("/cart/check-cart", {
      cart_id: e.target.dataset.cart_id,
      is_check: e.detail == true ? 1 : 0
    }).then(res => {
      if (res.result == 1) {
        this.setData({
          [`dataList[${e.target.dataset.index}].is_check`]: e.detail == true ? 1 : 0,
        });
        this.calculationPrice();
      }
    })
  },
  // 全选
  onallChange(e) {
    post("/cart/check-all-cart", {
      is_check: e.detail == true ? 1 : 0
    }).then(res => {
      if (res.result == 1) {
        let dataList = this.data.dataList.map((item) => {
          return {
            ...item,
            is_check: e.detail == true ? 1 : 0
          };
        });
        this.setData({
          dataList: dataList,
          allChecked: e.detail
        });
        this.calculationPrice();
      }
    })
  },
  // 计算价格,结算按钮是否开启
  calculationPrice() {
    const allPrice = this.data.dataList.filter(item => item.is_check == 1).reduce((total, num) => total + num.price * num.num, 0);
    const disabled = this.data.dataList.every(item => item.is_check == 0);
    const allChecked = this.data.dataList.length == 0 ? false : this.data.dataList.every(item => item.is_check == 1);
    this.setData({
      allPrice: allPrice,
      disabled: disabled,
      allChecked: allChecked
    });
  },
  // 删除
  delProduct(e) {
    wx.showModal({
      content: '您确定要删除此商品吗？',
      success: (res) => {
        if (res.confirm) {
          post("/cart/delete-product", {
            cart_id: e.target.dataset.cart_id
          }).then(res => {
            if (res.result == 1) {
              this.data.dataList.splice(e.target.dataset.index, 1);
              this.setData({
                dataList: this.data.dataList
              });
              this.calculationPrice();
            }
          });
        }
      }
    })
  },
  // 加减数量
  onChangeNum(e) {
    post("/cart/update-num", {
      cart_id: e.target.dataset.cart_id,
      num: e.detail
    }).then(res => {
      if (res.result == 1) {
        this.setData({
          [`dataList[${e.target.dataset.index}].num`]: e.detail
        });
        this.calculationPrice();
      }
    });
  },
  // 结算
  settlement(e) {
    if (e.detail.errMsg == "getUserInfo:ok") {
      get('/mine/user-info', e.detail.userInfo).then(res => {
        if (res.result == 1) {
          wx.navigateTo({
            url: `../paymentOrder/index`
          })
        }
      })
    };
  },
  onShow: function () {
    if (typeof this.getTabBar === 'function' &&
      this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2
      })
    };
    // 进来再次加载
    this.getList();
    // 控制分享按钮
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline'],
      success(res) {
        console.log('showShareMenu', res);
      }
    })
  }
})